import { log } from '../aop/log';
import BusinessDao, { IFormBusiness } from '../dao/BusinessDao';
import { BusinessFile, FileOptions, TTSTalk } from '../interface';
import { fetchTTSTalksWithPageSize, fetchBusinessMaterialWithPageSize } from '../Api';
import AgentServer from '@/modules/Agent';

enum IBusinessStatus {
	IDLE = 'idle',
	START = 'start',
	IN_PROGRESS = 'in-progress',
	REJECTED = 'rejected',
	CANCEL = 'cancel',
	FAILED = 'failed',
	DONE = 'done'
}

const uuid = (len: number, radix: number) => {
	let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	let uuid = [];
	let i;
	radix = radix || chars.length;

	if (len) {
		for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
	} else {
		var r;
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';
		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | (Math.random() * 16);
				uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
			}
		}
	}

	return uuid.join('');
};

class BusinessController {
	public getAllBusiness() {
		return BusinessDao.getAllBusiness();
	}

	public async fetchTTSTalksWithPageSize(params: { scenId: string | number; page: number; pageSize: number }) {
		try {
			const { data } = await fetchTTSTalksWithPageSize(params);
			if (data.code === 200) {
				BusinessDao.setTTSTalks([...data.data.data]);
				return data.data;
			}
			console.log('fetch tts talks success', data);
		} catch (error) {
			console.error('fetch tts talks error', error);
		}
	}

	public async fetchBusinessMaterialWithPageSize(params: { scenId: string | number; type: 'document' | 'video'; page: number; pageSize: number }) {
		const validateUrl = (url: string) => {
			if (!url) return '';
			if (!AgentServer.configure.awsFileUrl) return url;
			return `${AgentServer.configure.awsFileUrl}${url}`;
		};
		try {
			const { data } = await fetchBusinessMaterialWithPageSize(params);

			if (data.code === 200) {
				const list: BusinessFile[] = data.data.data || [];
				BusinessDao.setPdfFiles(list.filter(item => item.type === 'document').map(k => ({ id: k.id, name: k.fileName, title: k.title, size: k.fileSize, url: validateUrl(k.content) })));
				BusinessDao.setVideoFiles(list.filter(item => item.type === 'video').map(k => ({ id: k.id, name: k.fileName, title: k.title, size: k.fileSize, url: validateUrl(k.content) })));
			}

			console.log('fetch business material success', data, BusinessDao.getPdfFiles());
		} catch (error) {
			console.error('fetch business material error', error);
		}
	}

	public findBusiness(businessName: string) {
		return BusinessDao.findBusiness(businessName);
	}

	public updateBusinessStatus(businessName: string, status: IBusinessStatus) {
		const business = this.findBusiness(businessName);
		if (business) {
			BusinessDao.updateBusiness({ ...business, status });
		}
	}

	public getProgressBusiness() {
		return BusinessDao.findBusinessWithStatus(IBusinessStatus.IN_PROGRESS);
	}

	public setPdfFiles(files: FileOptions[]) {
		return BusinessDao.setPdfFiles(files);
	}

	public getPdfFiles() {
		return BusinessDao.getPdfFiles();
	}

	public setVideoFiles(files: FileOptions[]) {
		return BusinessDao.setVideoFiles(files);
	}
	public getVideoFiles() {
		return BusinessDao.getVideoFiles();
	}

	public setTTSTalks(talks: TTSTalk[]) {
		return BusinessDao.setTTSTalks(talks);
	}
	public getTTSTalks() {
		return BusinessDao.getTTSTalks();
	}

	public getFormDetail(followId: string) {
		return BusinessDao.getFormDetail(followId);
	}

	public insertFormDetail(clientId: string, forms: IFormBusiness) {
		return BusinessDao.insertFormDetail(clientId, forms);
	}

	public clearFormDetail() {
		return BusinessDao.clearFormDetail();
	}

	public getClientForms(clientId: string) {
		return BusinessDao.getClientForms(clientId);
	}
}

export default BusinessController;
